<?php

class User_model extends CI_Model {

    private $table = 'group6_tbl_user';
    private $tbl_role = 'group6_tbl_role';
    public $id = 0;
    public $role = '';
    public $role_id = 0;
    public $firstname = '';
    public $lastname = '';
    public $error = '';

    function __construct() {
        parent::__construct();
    }

    public function get_all() {
        return $this->db->get($this->table);
    }

    public function get_all_by_user_id($id) {
        $this->db->where('user_id', $id);
        $this->db->where('enable', true);
        return $this->db->get($this->table);
    }

    public function get_role() {
        return $this->db->get($this->tbl_role);
    }

    public function add_by_post($post) {
        $insert_data = [
            'role_id' => $post['role_id'],
            'username' => $post['username'],
            'password' => $post['password'],
            'firstname' => $post['fname'],
            'lastname' => $post['lname'],
            'email' => $post['email'],
            'contact_number' => $post['con_number'],
            'enable' => true
        ];

        $this->db->insert($this->table, $insert_data);
    }

    public function validate_firstname() {

        //$this->form_validation->set_message('validate_firstname', 'error firstname');
        //$this->form_validation->set_rules('firstname', 'trim|required|xss_clean', 'Mising');
    }

    // public function validate_lastname()
    // {
    // 	$this->form_validation->set_rules('lastname', 'trim|required|xss_clean', 'Mising');
    // }
    // public function validate_username()
    // {
    // 	$this->form_validation->set_rules('username', 'trim|required|xss_clean', 'Mising');
    // }
    // public function validate_email()
    // {
    // 	$this->form_validation->set_rules('email', 'trim|required|xss_clean', 'Mising');
    // }
    // public function validate_phone_number()
    // {
    // 	$this->form_validation->set_rules('phone_number', 'trim|required|xss_clean', 'Mising');
    // }
    // public function validate_password()
    // {
    // 	$this->form_validation->set_rules('password', 'trim|required|xss_clean', 'Mising');
    // }

    /**
     * @param array $post
     * 
     * @return bool
     */
    public function add_by_user_post($post) {
        $this->load->library('form_validation');

        //$this->validate_firstname();
        // echo '<pre>' . print_r($validattion, 1) . '</pre>';
        ///echo '<pre>' . print_r($post, 1) . '</pre>';
        ///exit;
        echo $this->form_validation->run();
        exit;


        //validattion here
        // $insert_data = [
        // 	'Firstname' => $post['firstname'],
        // 	'Lastname'	=> $post['lastname'].
        // 	'Username'	=> $post['username'],
        // 	'Email'		=> $post['email'],
        // 	'Password'	=> md5($post['password'])
        // ]; 		
        // $this->db->insert($this->table, $insert_data);
    }

    public function check_credentials($username, $password) {
        $this->db->select('user_id, firstname, lastname, group6_tbl_role.role_id, role_name')
                ->join('group6_tbl_role', 'group6_tbl_role.role_id = ' . $this->table . '.role_id')
                ->where('username', $username)
                ->where('password', md5($password))
                ->where($this->table . '.enable', true)
                ->limit(1);

        $query = $this->db->get($this->table);

        if ($query->num_rows() > 0) {
            $row = $query->row();
            $this->id = $row->user_id;
            $this->firstname = $row->firstname;
            $this->lastname = $row->lastname;
            $this->role_id = $row->role_id;
            $this->role = $row->role_name;

            return true;
        }

        return false;
    }

    public function error() {
        return $this->error;
    }

}